package com.pig4cloud.pig.yu.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.buyan.entity.CarModel;
import com.pig4cloud.pig.buyan.entity.Style;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.qc.entity.LabelEntity;
import com.pig4cloud.pig.yu.entity.CarBrandEntity;
import com.pig4cloud.pig.yu.entity.CarInfoEntity;
import com.pig4cloud.pig.yu.service.CarInfoService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 车辆信息管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/carInfo" )
@Tag(description = "carInfo" , name = "车辆信息管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CarInfoController {
    private final  CarInfoService carInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param carInfo 车辆信息管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('yu_carInfo_view')" )
    public R getCarInfoPage(@ParameterObject Page page, @ParameterObject CarInfoEntity carInfo,
							@RequestParam(required = false) BigDecimal minPrice,
							@RequestParam(required = false) BigDecimal maxPrice) {
		return R.ok(carInfoService.getCarInfoPage(page, carInfo,minPrice,maxPrice));
    }

    /**
     * 通过id查询车辆信息管理
     * @param infoId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{infoId}" )
    @PreAuthorize("@pms.hasPermission('yu_carInfo_view')" )
    public R getById(@PathVariable("infoId" ) Long infoId) {
		return R.ok(carInfoService.getCarInfoById(infoId));
    }

    /**
     * 新增车辆信息管理
     * @param carInfo 车辆信息管理
     * @return R
     */
    @Operation(summary = "新增车辆信息管理" , description = "新增车辆信息管理" )
    @SysLog("新增车辆信息管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('yu_carInfo_add')" )
    public R save(@RequestBody CarInfoEntity carInfo) {
		String carCode;
		do {
			String datePart = new SimpleDateFormat("yyyyMMdd").format(new Date());
			String randomPart = RandomStringUtils.randomNumeric(5);
			carCode = "CL" + datePart + randomPart;
		} while (carInfoService.existsByCarCode(carCode));
		carInfo.setCarCode(carCode);
		carInfo.setDelFlag(0);
		carInfo.setIsSelling("2");
        return R.ok(carInfoService.save(carInfo));
    }

    /**
     * 修改车辆信息管理
     * @param carInfo 车辆信息管理
     * @return R
     */
    @Operation(summary = "修改车辆信息管理" , description = "修改车辆信息管理" )
    @SysLog("修改车辆信息管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('yu_carInfo_edit')" )
    public R updateById(@RequestBody CarInfoEntity carInfo) {
		return R.ok(carInfoService.updateById(carInfo));
    }

    /**
     * 通过id删除车辆信息管理
     * @param ids infoId列表
     * @return R
     */
    @Operation(summary = "通过id删除车辆信息管理" , description = "通过id删除车辆信息管理" )
    @SysLog("通过id删除车辆信息管理" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('yu_carInfo_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(carInfoService.removeBatchByIds(CollUtil.toList(ids)));
    }

	/**
	 * 获取品牌
	 */
	@Operation(summary = "获取品牌", description = "获取品牌")
	@GetMapping("/brands")
	@PreAuthorize("@pms.hasPermission('yu_carInfo_view')")
	public R<List<CarBrandEntity>> getBrandsBySeries(@RequestParam String series) {
		return R.ok(carInfoService.getBrands(series));
	}

	/**
	 * 获取型号
	 */
	@Operation(summary = "获取型号", description = "获取型号")
	@GetMapping("/models")
	@PreAuthorize("@pms.hasPermission('yu_carInfo_view')")
	public R<List<CarModel>> getModelsByBrandId(@RequestParam Long brandId) {
		return R.ok(carInfoService.getModels(brandId));
	}

	/**
	 * 获取款式
	 */
	@Operation(summary = "获取款式", description = "获取款式")
	@GetMapping("/styles")
	@PreAuthorize("@pms.hasPermission('yu_carInfo_view')")
	public R<List<Style>> getStylesByModelId(@RequestParam  Long modelId) {
		return R.ok(carInfoService.getStyles(modelId));
	}

	/**
	 * 查询所有标签
	 */
	@Operation(summary = "获取所有标签", description = "获取所有标签")
	@GetMapping("/label")
	@PreAuthorize("@pms.hasPermission('yu_carInfo_view')")
	public R<List<LabelEntity>> getAllLabel() {
		return R.ok(carInfoService.getAllLabel());
	}
}